---------------------------------------------------------------
-- @Description：         四号位逻辑
---------------------------------------------------------------
local skynet = require "skynet"
local log = require "sky_log"
local uu = require "sky_utils"
local gg = require "game_utils"

local M = {}

local function do_round_color(handcards, main_color, round_color, outcard_history, outcard_history_more, m_seatid)
    local card 

    local max_card, maxcard_seatid = gg.get_prev_max_outcard(outcard_history_more, main_color)
    uu.debug_logic("do_round_color: ".."m_seatid="..(m_seatid or "nil")
            .." max_card="..(PUKE_CARDS[max_card] or "nil")
            ..(" max_card_seatid="..(maxcard_seatid or "nil"))
    )
   
    if not uu.check_is_friend(m_seatid, maxcard_seatid) then
        uu.debug_logic("the max_card is not friend!")
        card = gg.check_handcards_bigger_card_min_pure(handcards, max_card, round_color)
        if card then
            uu.debug_logic(1)
            return card
        end
    end
    
    card = uu.get_color_min_card(handcards, round_color)
    if not card then
        log.error("[fourth] get_color_min_card error!")
    end

    uu.debug_logic(2)
    return card
end

local function do_no_round_color(handcards, main_color, enemy_no_color_t, outcard_history, outcard_history_more, m_seatid)
    local card 
   
    local max_card, maxcard_seatid = gg.get_prev_max_outcard(outcard_history_more, main_color)
    if not uu.check_is_friend(m_seatid, maxcard_seatid) then 
        if not uu.history_check_the_max_card(handcards, outcard_history, max_card) then
            card = uu.check_handcards_bigger_card_min(handcards, max_card, main_color)
            if card then
                uu.debug_logic(3)
                return card
            end
        else
            --主牌也是大于本回合中最大的牌
            card = uu.get_color_min_card(handcards, main_color)
            if card then
                return card
            end
        end
    end 

    local the_color = gg.check_enemy_has_no_color_s_and_main_card(handcards, {1,2,3,4}, enemy_no_color_t, main_color)
    if the_color then 
        local _max_card = uu.history_check_max_card(handcards, the_color, outcard_history)
        local count = uu.get_color_cards_count(handcards, the_color)
        if _max_card and count >= 2 then
            card = uu.get_color_min_card(handcards, the_color)
            if card then
                uu.debug_logic(4)
                return card
            else
                log.error("[fourth] get_color_min_card error!")
            end
        end
    end

    card = gg.history_check_more_color_or_min_card_expect_maincards(handcards, outcard_history, main_color)
    if not card then
        card = uu.get_color_min_card(handcards, main_color)
    end

    uu.debug_logic(5)
    return card
end

function M.do_card(handcards, main_color, round_color, enemy_no_color_t, outcard_history, outcard_history_more, m_seatid)
    if __LOG_LEVEL___==1 then
        local tmp = {}
        for _, v in ipairs(handcards) do
            table.insert(tmp, string.format("%04X", v))
        end
        uu.debug_logic(tmp)
    end
   
    local card

    if gg.check_has_round_color(handcards, round_color) then 
        card = do_round_color(handcards, main_color, round_color, outcard_history, outcard_history_more, m_seatid)
    else
        card = do_no_round_color(handcards, main_color, enemy_no_color_t, outcard_history, outcard_history_more, m_seatid)
    end

    --最后的容错
    local the_flag, the_card = gg.check_error_card(handcards, card, round_color, 4)
    if the_flag==true then
        uu.debug_logic(6)
        card = the_card
    end

    return card
end


return M